import { getStorageSync } from "@tarojs/taro"
import Taro from "@tarojs/taro"

const ISDEBUG = true

const HOST = ISDEBUG ? 'https://yinyonghui.asia' : 'https://yinyonghui.asia'


function addTimeStr(m) {
  return m < 10 ? '0' + m : m
}

function diffTime(time) {
  let result = new Date().getTime() - time
  if (result <= 60000 && result > 0) {
    return '刚刚'
  } else if (result <= 3600000 && result > 60000) {
    return Math.floor((result % 3600000) / 60000) + '分钟前'
  } else if (result <= 86400000 && result > 3600000) {
    return Math.floor((result % 86400000) / 3600000) + '小时前'
  } else {
    let year = new Date().getFullYear()
    let oldYear = new Date(time).getFullYear()
    let oldTime = new Date(time)
    if (year == oldYear) {
      let m = oldTime.getMonth() + 1
      let d = oldTime.getDate()
      let h = oldTime.getHours()
      let mm = oldTime.getMinutes()
      // return addTimeStr(m) + '-' + addTimeStr(d) + ' ' + addTimeStr(h) + ':' + addTimeStr(mm)
      return addTimeStr(m) + '-' + addTimeStr(d)
    } else {
      let y = oldTime.getFullYear()
      let m = oldTime.getMonth() + 1
      let d = oldTime.getDate()
      let h = oldTime.getHours()
      let mm = oldTime.getMinutes()
      // return y + '-' + addTimeStr(m) + '-' + addTimeStr(d) + ' ' + addTimeStr(h) + ':' + addTimeStr(mm)
      return y + '-' + addTimeStr(m) + '-' + addTimeStr(d)
    }
  }
}


function checkLogin() {
  if (getStorageSync('USERINFO')) {
    return true
  }
  return false
}


function getUserInfo() {
  let data = {}
  if (getStorageSync('USERINFO')) {
    data = JSON.parse(getStorageSync('USERINFO'))
  }
  return data
}

function judgeLogin() {
  if (getStorageSync('USERINFO')) {
     return true
  }else{
    return  false
  }
}


function getRandomElements(arr, count) {
  const result = new Array(count);
  const taken = new Set();

  while (--count >= 0) {
    const index = Math.floor(Math.random() * arr.length);
    result[count] = arr[index in taken ? taken[index] : index];
    taken.add(index);
  }
  return result;
}

export {
  HOST,
  diffTime,
  checkLogin,
  getUserInfo,
  getRandomElements,
  judgeLogin
}